package online.lona.mc.mod.ymer;

import online.lona.mc.NativeInterface;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerMessageQueue {

    private Queue<byte[]> messageQueue = new ConcurrentLinkedQueue<>();
    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    public void newMessageIncoming(byte[] message) {
        messageQueue.add(message);
        executorService.submit(this::processMessageQueue);
    }

    private void processMessageQueue() {
        while (!messageQueue.isEmpty()) {
            byte[] message = messageQueue.poll();
            NativeInterface.getInstance().onServerMessage(message);
        }
    }
}
